package com.video.web.controller.system;

import java.util.List;

import com.video.system.domain.VideoEducation;
import com.video.system.domain.VideoRole;
import com.video.system.service.IVideoEducationService;
import com.video.system.service.IVideoRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.video.common.annotation.Log;
import com.video.common.enums.BusinessType;
import com.video.system.domain.VideoUser;
import com.video.system.service.IVideoUserService;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author lyh
 * @date 2021-07-20
 */
@Controller
@RequestMapping("/system/u_user")
public class VideoUserController extends BaseController
{
    private String prefix = "system/u_user";

    @Autowired
    private IVideoUserService videoUserService;
    @Autowired
    private IVideoRoleService videoRoleService;
    @Autowired
    private IVideoEducationService videoEducationService;


    @RequiresPermissions("system:u_user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询用户列表
     */
    @RequiresPermissions("system:u_user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VideoUser videoUser)
    {
        startPage();
        List<VideoUser> list = videoUserService.selectVideoUserList(videoUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("system:u_user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VideoUser videoUser)
    {
        List<VideoUser> list = videoUserService.selectVideoUserList(videoUser);
        ExcelUtil<VideoUser> util = new ExcelUtil<VideoUser>(VideoUser.class);
        return util.exportExcel(list, "用户数据");
    }


    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        //查询用户角色和用户学历
        List<VideoRole> videoRoles = videoRoleService.selectRoleAll();
        List<VideoEducation> videoEducations = videoEducationService.selectCodeAndNameAll();
        map.put("all1",videoEducations);
        map.put("all",videoRoles);
        System.out.println("=================================>videoRoles"+videoRoles);
        System.out.println("================================>videoEducations"+videoEducations);

        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:u_user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VideoUser videoUser)
    {
        return toAjax(videoUserService.insertVideoUser(videoUser));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{uId}")
    public String edit(@PathVariable("uId") Integer uId, ModelMap mmap)
    {
        VideoUser videoUser = videoUserService.selectVideoUserById(uId);
        mmap.put("videoUser", videoUser);

        //查询用户角色和用户学历
        List<VideoRole> videoRoles = videoRoleService.selectRoleAll();
        List<VideoEducation> videoEducations = videoEducationService.selectCodeAndNameAll();
        mmap.put("all", videoRoles);
        mmap.put("all1", videoEducations);

        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:u_user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VideoUser videoUser)
    {
        return toAjax(videoUserService.updateVideoUser(videoUser));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("system:u_user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(videoUserService.deleteVideoUserByIds(ids));
    }
}
